import axios from 'axios'
import { SET_CURRENT_USER, GET_PROFILE, GET_PROFILES, PROFILE_LOADING, CLEAR_CURRENT_PROFILE, GET_ERRORS } from './types'
// 用户个人信息数据
export const getCurrentProfile = () => dispatch => {
  // 加载动画
  dispatch(setProfileLoading())
  // 请求登录用户个人信息数据
  axios('/api/profile').then(res => {
    //请求成功把数据返回出去
    dispatch({
      type: GET_PROFILE,
      payload: res.data
    })
  }).catch(err => {
    dispatch({
      type: GET_PROFILE,
      payload: {}
    })
  })
}
// 获取所有开发人员信息数据
export const getProfiles = () => dispatch => {
  // 加载动画
  dispatch(setProfileLoading())
  // 请求所有开发人员信息数据
  axios('/api/profile/all').then(res => {
    //请求成功把数据返回出去
    dispatch({
      type: GET_PROFILES,
      payload: res.data
    })
  }).catch(err => {
    dispatch({
      type: GET_PROFILES,
      payload: null
    })
  })
}
// 加载动画
export const setProfileLoading = () => {
  return {
    type: PROFILE_LOADING
  }
}
// 清除用户信息
export const clearCurrentProfile = () => {
  return {
    type: CLEAR_CURRENT_PROFILE
  }
}
// 创建个人信息
export const createProfile = (profileData, history) => dispatch => {
  // 加载动画
  dispatch(setProfileLoading())
  axios.post("/api/profile", profileData)
    .then(res => {
      console.log('profileData', profileData);
      history.push("/dashboard")
    })
    .catch(err => {
      console.log(err)
      dispatch({
        type: GET_ERRORS,
        payload: err.response.data
      })
    }
    )
}
// 删除帐号
export const deleteAccout = (params) => dispatch => {
  // 加载动画
  dispatch(setProfileLoading())
  axios.delete("/api/profile")
    .then(res =>
      dispatch({
        type: SET_CURRENT_USER,
        payload: {}
      })
    )
    .catch(err =>
      dispatch({
        type: GET_ERRORS,
        payload: err.response.data
      })
    )

}
// 添加个人经历
export const addExperience = (expData, history) => dispatch => {
  // 加载动画
  dispatch(setProfileLoading())
  axios.post("/api/profile/experience", expData)
    .then(res => history.push("/dashboard"))
    .catch(err =>
      dispatch({
        type: GET_ERRORS,
        payload: err.response.data
      })
    )
}
// 添加教育
export const addEducation = (expData, history) => dispatch => {
  axios.post("/api/profile/education", expData)
    .then(res => history.push("/dashboard"))
    .catch(err =>
      dispatch({
        type: GET_ERRORS,
        payload: err.response.data
      })
    )
}
// 删除教育
export const deleteEducation = id => dispatch => {
  // 加载动画
  dispatch(setProfileLoading())
  axios.delete(`/api/profile/education/${id}`)
    .then(res =>
      dispatch({
        type: GET_PROFILE,
        payload: res.data
      })
    )
    .catch(err =>
      dispatch({
        type: GET_ERRORS,
        payload: err.response.data
      })
    )
}
// 删除个人履历
export const deleteExperience = id => dispatch => {
  // 加载动画
  dispatch(setProfileLoading())
  axios.delete(`/api/profile/experience/${id}`)
    .then(res =>
      dispatch({
        type: GET_PROFILE,
        payload: res.data
      })
    )
    .catch(err =>
      dispatch({
        type: GET_ERRORS,
        payload: err.response.data
      })
    )
}
// 根据动态路由handle获取开发者更多信息
export const getProfileByHandle = handle => dispatch => {
  // 加载动画
  dispatch(setProfileLoading())
  axios(`/api/profile/handle/${handle}`)
    .then(res =>
      dispatch({
        type: GET_PROFILE,
        payload: res.data
      })
    )
    .catch(err =>
      dispatch({
        type: GET_PROFILE,
        payload: null
      })
    )
}
